home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Files / FileWriter.cp < prev    next >
Text File  |  1997-06-28  |  2KB  |  78 lines

  1. // FileWriter.cp
  2.  
  3. #ifndef FileWriter_h
  4. #include "FileWriter.h"
  5. #endif
  6. #ifndef ConstBuffer_h
  7. #include "ConstBuffer.h"
  8. #endif
  9. #ifndef FileAccessPath_h
  10. #include "FileAccessPath.h"
  11. #endif
  12. #ifndef DiskFullError_h
  13. #include "DiskFullError.h"
  14. #endif
  15. #ifndef HardwareVolumeLockError_h
  16. #include "HardwareVolumeLockError.h"
  17. #endif
  18. #ifndef SoftwareVolumeLockError_h
  19. #include "SoftwareVolumeLockError.h"
  20. #endif
  21. #ifndef FileLockError_h
  22. #include "FileLockError.h"
  23. #endif
  24.  
  25. FileWriter::FileWriter( const FileAccessPath& theFile,
  26.                                 uint32 position )
  27.   {
  28.     Assert( theFile.IsOpen() );
  29.     
  30.     ioParam.ioCompletion = 0;
  31.     ioParam.ioRefNum = theFile.RefNum();
  32.     ioParam.ioPosMode = fsFromStart;
  33.     ioParam.ioPosOffset = position;
  34.   }
  35.  
  36. uint32 FileWriter::operator<<( ConstBuffer& buffer )
  37.   {
  38.     ioParam.ioBuffer = reinterpret_cast<int8 *>(
  39.                                 const_cast<uint8 *>(
  40.                                     buffer.Unused().Start() ) );
  41.     ioParam.ioReqCount = buffer.Unused().Length();
  42.     
  43.     OSErr error = PBWriteSync( this );
  44.     buffer.AdvanceMark( ioParam.ioActCount );
  45.     
  46.     ThrowError( error );
  47.     
  48.     return ioParam.ioActCount;
  49.   }
  50.  
  51. void FileWriter::operator<<( ConstData data )
  52.   {
  53.     ioParam.ioBuffer = reinterpret_cast<int8 *>(
  54.                                 const_cast<uint8 *>(
  55.                                     data.Start() ) );
  56.     ioParam.ioReqCount = data.Length();
  57.     
  58.     ThrowError( PBWriteSync( this ) );
  59.     
  60.     Assert( ioParam.ioActCount == data.Length() );
  61.   }
  62.  
  63. void FileWriter::ThrowError( OSErr error )
  64.   {
  65.     if ( error == noErr )
  66.         return;
  67.     
  68.     switch ( error )
  69.       {
  70.         case dskFulErr:            throw DiskFullError( error );
  71.         case wPrErr:                throw HardwareVolumeLockError( error );
  72.         case vLckdErr:                throw SoftwareVolumeLockError( error );
  73.         case fLckdErr:                throw FileLockError( error );
  74.       }
  75.     
  76.     throw FileError( error );
  77.   }
  78.